package com.leyou.item.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.leyou.item.dto.CategoryDTO;
import com.leyou.item.entity.Category;
import com.leyou.item.entity.CategoryBrand;
import com.leyou.item.mapper.CategoryMapper;
import com.leyou.item.service.CategoryBrandService;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

@Service
/*继承了service的实现类间接实现了service的方法
 */
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryBrandService categoryBrandService;
    @Override
    /**
     * 根据品牌id查询分类
     */
    public List<CategoryDTO> queryCategoryByBrandId(long bid) {
        //根据品牌id查询中间表，获取中间表对象集合
        List<CategoryBrand> brandList = this.categoryBrandService.query().eq("brand_id", bid).list();
        //判断查询到的集合是否为空，为空则返回一个空集合
        if(CollectionUtils.isEmpty(brandList)){
            return Collections.emptyList();
        }
        //获取分类id集合
        List<Long> categoryIds = brandList.stream().map(CategoryBrand::getCategoryId).collect(Collectors.toList());
        //根据中间表中的分类id查询商品
        List<Category> categories = listByIds(categoryIds);
        return CategoryDTO.convertEntityList(categories);
    }

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public List<Map<String,Object>> findAllCategory() throws Exception {
        //先查找所有的一级目录
        List<Category> firstCat = this.query().eq("parent_id", 0).list();

        //根据一级目录id查找所有的二级目录
//        for (Category category : firstCat) {
        List<Map<String, Object>> list = firstCat.stream().map(category -> {

            HashMap<String, Object> map = new LinkedHashMap<>();
            map.put("id", category.getId());
            map.put("name", category.getName());
            List<Category> sendcondList = this.query().eq("parent_id", category.getId()).list();
            //根据二级目录id查找三级目录
            List<HashMap<String, Object>> secondList = sendcondList.stream().map(category1 -> {
                HashMap<String, Object> map1 = new LinkedHashMap<>();
                map1.put("id", category1.getId());
                map1.put("name", category1.getName());
                List<Category> third = this.query().eq("parent_id", category1.getId()).list();
                //遍历三级目录，将数据封装到集合
                List<HashMap<String, Object>> thirdList = third.stream().map(category2 -> {
                    HashMap<String, Object> map2 = new HashMap<>();
                    map2.put("id", category2.getId());
                    map2.put("name", category2.getName());
                    return map2;
                }).collect(Collectors.toList());

                map1.put("son", thirdList);
                return map1;
            }).collect(Collectors.toList());
            map.put("son", secondList);
            return map;
        }).collect(Collectors.toList());
        ObjectMapper mapper = new ObjectMapper();
        String listStr = mapper.writeValueAsString(list);
        redisTemplate.opsForValue().set("all:category",listStr,24, TimeUnit.HOURS);
        return list;
    }

}
